/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modulo3;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import Modelo.DAL.NotificacionRiesgo_DAO;
import Acceso.Security;
import java.io.PrintWriter;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author h-m
 */
public class consultarNRAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI"};
        if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }

        String nombre_puesto = request.getParameter("nombre_puesto");
        String ci = request.getParameter("ci");
        String nacionalidad = request.getParameter("nacionalidad");
        String realizado_por = request.getParameter("realizado_por");
        String fecha_notificacion = request.getParameter("fecha_notificacion");
        String status = request.getParameter("status");
        String path = request.getParameter("path");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String limite = "";


        String atributos = " * ";
        String from = " from notificacion_riesgo ";
        String select = "Select ";
        String selectCount = "Select count(*) ";
        String where = " where 1 = 1 ";


        if (ci != null && !ci.equals("") && nacionalidad != null && !nacionalidad.equals("")) {
            nacionalidad += "-"+ci;
            where += " and ci = '" + nacionalidad + "'";
        }

        if (nombre_puesto != null && !nombre_puesto.equals("")) {
            where += " and lower(nombre_puesto) like lower('%"+nombre_puesto+"%')";
        }

        if (realizado_por != null && !realizado_por.equals("")) {
            where += " and lower(realizado_por) like lower('%"+realizado_por+"%')";
        }

        if (status != null && !status.equals("")) {
            where += " and status = '" + status + "'";
        }

        if (!limit.equals("0")) {
            limite += " limit " + limit + " offset " + offset;
        }

        if (order != null && !order.equals("")) {
            select += atributos + ", ( " + selectCount + from + where + " ) "  + from + where +order + limite+ ";";
        }
        else{
            select += atributos + ", ( " + selectCount + from + where + " ) " + from + where +limite+ ";";
        }
        
//        select += " ( " + selectCount + from + where + " ) , " + atributos + from + where + ";";

        System.out.println("select   "+select);
        response.setContentType("text/json");
        JSONArray nr = NotificacionRiesgo_DAO.consultarNR(select);
        if (nr == null) {
            return mapping.findForward(FAILURE);
        }
        JSONObject ext = new JSONObject();
        ext.put("consultarNR", nr);
        System.out.println("JSON :"+nr.toString());
        PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
